package com.chang.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chang.reggie.common.CustomException;
import com.chang.reggie.entity.Category;
import com.chang.reggie.entity.Dish;
import com.chang.reggie.entity.Setmeal;
import com.chang.reggie.mapper.CategoryMapper;
import com.chang.reggie.service.CategoryService;
import com.chang.reggie.service.DishService;
import com.chang.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishQueryWrapper = new LambdaQueryWrapper<>();
        //添加查询条件,根据id进行查询
        dishQueryWrapper.eq(Dish::getCategoryId,id);
        long dishCount = dishService.count(dishQueryWrapper);

        if (dishCount>0){
            throw new CustomException("已经关联菜品，不能删除！");
        }

        LambdaQueryWrapper<Setmeal> setmealQueryWrapper = new LambdaQueryWrapper<>();
        setmealQueryWrapper.eq(Setmeal::getCategoryId,id);
        long setmealCount = setmealService.count(setmealQueryWrapper);

        if (setmealCount>0){
            throw new CustomException("已经关联套餐，不能删除！");
        }
        //正常删除关联套餐
        super.removeById(id);
    }
}
